<?php

 class __module_admin_install {
    
    function step1() {
        return __driver_display::si()->fetch(array('install/step1.html', new KT_start_moduleName('admin')));
    }
    
    function step2() {
        /* Зачем делать эту глупую проверку? Если версия ниже - эта страница не запустится физически...
        $phpver = phpversion();
        $phpver = (double)($phpver[0].'.'.$phpver[2]);
        if($phpver >= 5.2) $s['phpver'] = true;
        else $s['phpver'] = false;
        
        #$s['mysql']  = true;
        */
        
        $s['mods']   = array(
            'sys'         => is_writable('./sys'),
            'templates_c' => is_writable('./templates_c'),
            'themes'      => is_writable('./themes'),
            'modules'     => is_writable('./modules'),
            'open'        => is_writable('./open'),
            'libs'        => is_writable('./libs'),
            'index'       => is_writable('./')
        );
        
        $actualver = __kernel_remote_api::getData( new KT_remote_server_getData('seng.org.ua', 80, false, '__module_server', 'getSEngVersion') );
        if(__kernel_api::getKernelVer() != $actualver) {
            $s['sengactualver'] = false;
            $s['sengactualver_me'] = __kernel_api::getKernelVer();
            $s['sengactualver_server'] = $actualver;
        } else $s['sengactualver'] = true;
        
        if($s['mods']['sys'] && $s['mods']['templates_c'] && $s['mods']['themes'] && $s['mods']['modules'] && $s['mods']['open'] && $s['mods']['libs'] && $s['mods']['index'] && $s['sengactualver'])
            $s['all_system_go'] = true;
        else $s['all_system_go'] = false;
        
        __driver_display::si()->assign('s', $s);
        return __driver_display::si()->fetch(array('install/step2.html', new KT_start_moduleName('admin')));
    }
    
    function step3() {
        $ip = ini_get('include_path');
        if($ip == str_replace(';',':', $ip)) { // *nix like OS
            $ip_delimetr = ':';
        } else { // Windows fake OS
            $ip_delimetr = ';';
        }
        __driver_display::si()->assign('os', $ip_delimetr);
        
        $ip_ex = explode($ip_delimetr, $ip);
        foreach($ip_ex as $item) {
            if(strtolower($item) != str_replace('pear', '', strtolower($item))) {
                $ppe = false;
                
                if(file_exists($item.'/DB.php')) {
                    __driver_display::si()->assign('pear_db', true);
                    $ppe = true;
                }
                
                __driver_display::si()->assign('pear_pack_enable', $ppe);
                __driver_display::si()->assign('pear_patch', $item);
                break;
            }
        }
        
        return __driver_display::si()->fetch(array('install/step3.html', new KT_start_moduleName('admin')));
    }
    
    function step4() {
        ini_set('include_path', implode($_POST['hosting']['os'], array('.', '..', './libs', $_POST['hosting']['pear'])));
        
        # Connect to MySQL
        unset($GLOBALS['config']['to_install']);
        if(__driver_db::si(array(
                "login" => $_POST['mysql']['login'],
                "pass"  => $_POST['mysql']['pass'], 
                "host"  => $_POST['mysql']['host'], 
                "dbname" => $_POST['mysql']['dbname'],
                "return_fail" => true
        )) == false) exit(glang(array('admin', 'install'), 'Failed connect to DB. Go back and change MySQL settings!'));
        
        # Install sql
        require('./sys/seng.sql.php');
        
        $ret['sql'] = 0;
        foreach($sql as $query) {
            $res = __driver_db::si()->query($query);
            if (PEAR::isError($res)) {
                print_r($res);
                __driver_db::si()->rollback();
                exit();
            }
            $ret['sql']++;
        }
        __driver_db::si()->commit();
        
        # Make users
        
        $res = __driver_db::si()->autoExecute("users", array(
				        "login" => $_POST['root']['login'],
				        "pwd"   => md5($_POST['root']['pass'])
				), DB_AUTOQUERY_INSERT);
				
		if (PEAR::isError($res)) {
            $ret['root'] = false;
        } else {
            __driver_db::si()->commit();
            $ret['root'] = true;
        }
		
		$res = __driver_db::si()->autoExecute("users", array(
				        "login" => $_POST['user']['login'],
				        "pwd"   => md5($_POST['user']['pass'])
				), DB_AUTOQUERY_INSERT);		
        
        if (PEAR::isError($res)) {
            $ret['user'] = false;
        } else {
            __driver_db::si()->commit();
            $ret['user'] = true;
        }
        
        $config = implode(file('./sys/config.template'));
        $config = str_replace('{include_patch}', ini_get('include_path'), $config);
        $config = str_replace('{underReconstruction}', 'true', $config);
        $config = str_replace('{db_login}', $_POST['mysql']['login'], $config);
        $config = str_replace('{db_pass}', $_POST['mysql']['pass'], $config);
        $config = str_replace('{db_host}', $_POST['mysql']['host'], $config);
        $config = str_replace('{db_name}', $_POST['mysql']['dbname'], $config);
        
        $fw = fopen('config.php', 'w');
        fwrite($fw, $config);
        fclose($fw);
        $ret['config'] = true;
        
        return __driver_display::si()->fetch(array('install/step4.html', new KT_start_moduleName('admin')));
    }
    
 }

?>
